package com.ssyc.gsk_master.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.ssyc.common.base.LazyBaseFragment;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.gsk_master.R;
import com.ssyc.gsk_master.activity.MasterCreatRedPackageActivity;
import com.ssyc.gsk_master.activity.MasterLivingListActivity;
import com.ssyc.gsk_master.activity.MasterManagerTeachersActivity;
import com.ssyc.gsk_master.activity.MasterRedPackageListActivity;
import com.ssyc.gsk_master.adapter.HomeGvAdapter;
import com.ssyc.gsk_master.bean.HomeInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 主页
 * Created by Administrator on 2018/7/13 0013.
 */

public class MasterHomeFragment extends LazyBaseFragment implements AdapterView.OnItemClickListener {


    //展示选项的GridView
    private GridView mGv;

    //首页的展示的条目的数据集合
    private List<HomeInfo> homeInfos;

    //适配器对象
    private HomeGvAdapter adapter;

    //红包
    private ImageView iv_redbag;

//    //首页的栏目的所有图片
//    private int[] itemResId = {R.drawable.master_jsgl, R.drawable.master_jxfs, R.drawable.master_jsfc, R.drawable.master_jzpj, R.drawable.master_stzb,
//            R.drawable.master_hbjl};
//    //首页的栏目名称
//    private String[] itemNames = {"教师管理", "教学与反思", "教师风采", "家长评价", "收听直播",
//            "红包记录"};

    //首页的栏目的所有图片
    private int[] itemResId = {R.drawable.master_jsgl, R.drawable.master_jsfc, R.drawable.master_jzpj, R.drawable.master_stzb,
            R.drawable.master_hbjl};

    //首页的栏目名称
    private String[] itemNames = {"教师管理", "教师风采", "家长评价", "收听直播",
            "红包记录"};


    public static final String TAG = MasterHomeFragment.class.getName();


    //单体模式
    private static class LazyHolder {
        private static final MasterHomeFragment INSTANCE = new MasterHomeFragment();
    }

    public static final MasterHomeFragment newInstance() {
        return LazyHolder.INSTANCE;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.master_fragment_home;
    }

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected void init(View view, Bundle savedInstanceState) {
        super.init(view, savedInstanceState);
        showContent();

        //初始化视图
        initView(view);

        //初始化数据
        initData();

        //初始化列表
        initGv();
    }


    /**
     * 初始化视图
     */
    private void initView(View view) {
        mGv = view.findViewById(R.id.gv);

        iv_redbag = view.findViewById(R.id.iv_redbag);
        iv_redbag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UiUtils.startActivity(getActivity(), MasterCreatRedPackageActivity.class);
            }
        });

    }

    /**
     * 初始化数据
     */
    private void initData() {
        homeInfos = new ArrayList<>();
        for (int i = 0; i < itemNames.length; i++) {
            HomeInfo info = new HomeInfo();
            info.resId = itemResId[i];
            info.name = itemNames[i];
            homeInfos.add(info);
        }
    }

    /**
     * 初始化列表的方法
     */
    private void initGv() {
        adapter = new HomeGvAdapter(getContext(), homeInfos, R.layout.master_gv_home);
        mGv.setAdapter(adapter);
        mGv.setOnItemClickListener(this);
    }

    /**
     * 列表的条目点击事件
     *
     * @param adapterView
     * @param view
     * @param pos
     * @param l
     */
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
        if (pos == 0) {
            //教师管理
            Intent intent = new Intent(getContext(), MasterManagerTeachersActivity.class);
            intent.putExtra("type", "1");
            startActivity(intent);
        }
//        else if (pos == ) {
        //教学与反思
//            Intent intent = new Intent(getContext(), MasterManagerTeachersActivity.class);
//            intent.putExtra("type", "2");
//            startActivity(intent);

        //开发中提示框
//            AlertDialogUtil.showAlertDialog(getActivity(), "程序GG正在努力开发中", new AlertDialogUtil.onClickListener() {
//                @Override
//                public void onClickOk() {
//                    CustomDialogManager.dissmiss();
//                }
//            });
//        }
        else if (pos == 1) {
            //教师风采
            ARouter.getInstance().build("/teacher/TeacherShowActivity").navigation();
        } else if (pos == 2) {
            //家长评价
            Intent intent = new Intent(getContext(), MasterManagerTeachersActivity.class);
            intent.putExtra("type", "3");
            startActivity(intent);
        } else if (pos == 3) {
            //收听直播
            UiUtils.startActivity(getActivity(), MasterLivingListActivity.class);
        } else if (pos == 4) {
            //红包记录
            UiUtils.startActivity(getActivity(), MasterRedPackageListActivity.class);
        }
    }
}
